<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/6
 * Time: 10:16
 */
namespace App\Http\Controllers\admin_system;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Module\EntAudioDay;
use App\Module\EntManageuser;
use Storage;
use Cookie;
use DB;
use Validator;
use Redirect;
use Session;

class AudioDayController extends Controller
{
    public function index($keyword = '')
    {
        if ($keyword != '') {
            $audioDay = DB::table('vw_audioday')->select('AudioID', 'AudioTitle', 'IsShow','MasterID', 'ReleaseDate', 'RealName','PublishDate')
                ->where('DeleteMark', '1')
                ->where('AudioTitle', 'like', '%' . $keyword . '%')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        } else {
            $audioDay = DB::table('vw_audioday')->select('AudioID', 'AudioTitle', 'IsShow', 'MasterID', 'ReleaseDate', 'RealName','PublishDate')
                ->where('DeleteMark', '1')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        }


        return view('admin_system.audioDay.index', [
            'audioDay' => $audioDay,
            'keyword' => $keyword
        ]);
    }



    public function createindex(Request $request)
    {
        $audioId    = $request->input('id');
        if($audioId) {

            $audio = EntAudioDay::findOrFail($audioId);

        }else{

            $audio    = new \stdClass();
            $audio->AudioTitle    = "";
            $audio->FilePath1     = "";
            $audio->time_length   = "";
            $audio->PublishDate   = current_date();
            $audio->IsShow        = 1;
            $audio->AudioID       = 0;
        }

        return view('admin_system.audioDay.indexAdd',['audio'=>$audio]);
    }



    /*
     * 添加音频信息
     * */
    public function storeindex(Request $request)
    {
        $AName = Session::get('AName');
        $AKey = Session::get('AKey');
        if ($AKey != '' && $AName != '')
        {
            $entManageuser = EntManageuser::select('UserID')->where('UserName', $AName)
                ->where('UserPwd', md5($AKey))
                ->where('DeleteMark', 1)
                ->first();
        }

        $audioId    = $request->input('audioId');

        if($audioId > 0)
        {
            $entAudioDay    = EntAudioDay::findOrFail($audioId);

        }else{

            $entAudioDay            = new EntAudioDay();
            $entAudioDay->AddDate   = date('Y-m-d H:i:s', time());

        }

        $entAudioDay->AudioTitle    = trim($request->get('txtAudioTitle'));
        $entAudioDay->FilePath1     = trim($request->get('txtAudioUrl'));
        $entAudioDay->time_length   = trim($request->get('time_length'));
        $entAudioDay->PublishDate   = trim($request->get('PublishDate'));
        $entAudioDay->MasterID      = $entManageuser->UserID;
        $entAudioDay->DeleteMark    = 1;
        $entAudioDay->IsShow        = $request->get('RadIsShow');
        $entAudioDay->ReleaseDate   = date('Y-m-d H:i:s', time());
        $entAudioDay->save();

        if ($entAudioDay) {
            return apiData()->notice('操作成功');
        } else {
            return apiData()->notice('操作失败');
        }
    }


    /*
     * 删除音频信息
     * */
    public function indexdelete(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $IDList = $request->json('IDList');
                if (is_numeric($IDList)) {
                    EntAudioDay::where('AudioID', $IDList)->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                } else {
                    EntAudioDay::whereIn('AudioID', explode(',', $IDList))->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                }
            }
        } catch (Exception $e) {
            $result = 'no';
        }
        return $result;
    }
}